/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.StaffMemberMenuController;
import java.awt.GridBagConstraints;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import model.Base;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Ira
 */
public class StaffMemberView extends View {
	
	public static final String MENU_DECO = "menu_deco";
	public static final String MENU_QUIT = "menu_quit";
	public static final String MENU_ADD_USER = "menu_add_user";
	public static final String MENU_SEARCH_USER = "menu_search_user";
	public static final String MENU_ADD_DOC = "menu_add_doc";
	public static final String MENU_SEARCH_DOC = "menu_search_doc";
	public static final String MENU_ABOUT = "menu_about";
	public static final String MENU_CHANGE_PASS = "menu_change_pass";
	
	private MainWindow mainWindow;
	private SearchView searchView;
	private DocumentResultView resultDocView;
	private UserResultView resultUserView;
	private DocumentPropertiesView propertiesDocView;
	private UserPropertiesView propertiesUserView;
	private BorrowDocumentView borrowView;
	private GridBagConstraints ctrResultView = new GridBagConstraints();
	private GridBagConstraints ctrPropertiesView = new GridBagConstraints();
	private Base model;
	
	public StaffMemberView(Base model, MainWindow mainWindow) {
		
		this.model = model;
		
		this.ctrResultView.gridx = 0;
		this.ctrResultView.gridy = 0;
		this.ctrResultView.weightx = 1.0;
		this.ctrResultView.weighty = 1.0;
		this.ctrResultView.fill = GridBagConstraints.BOTH;
		
		this.ctrPropertiesView.gridx = 0;
		this.ctrPropertiesView.gridy = 1;
		
		this.mainWindow = mainWindow;
		this.setMenu(model);
		
		this.searchView = new SearchView(model, true);
		this.resultDocView = new DocumentResultView(this);
		this.resultUserView = new UserResultView(this);
		this.propertiesDocView = new DocumentPropertiesView(model, true, this);
		this.propertiesUserView = new UserPropertiesView(model, this);
		this.borrowView = new BorrowDocumentView(model);
		
		// Observers
		model.addObserver(this.borrowView);
		model.addObserver(this.resultDocView);
		model.addObserver(this.resultUserView);
		this.resultDocView.addObserver(this.propertiesDocView);
		this.resultUserView.addObserver(this.propertiesUserView);
		this.resultUserView.addObserver(this.borrowView);
		this.borrowView.addObserver(this.propertiesDocView);
		
		GridBagConstraints constraint = new GridBagConstraints();
		constraint.anchor = GridBagConstraints.NORTH;
		
		// Vue des résultats
		constraint.gridx = 0;
		constraint.gridy = 0;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.fill = GridBagConstraints.BOTH;
		this.add(this.resultDocView, constraint);
		
		// Vue de recherche
		constraint.gridx = 1;
		constraint.weightx = 0.0;
		constraint.weighty = 0.0;
		this.add(this.searchView, constraint);
		
		// Vue des documents empruntés
		constraint.gridy = 1;
		this.add(this.borrowView, constraint);
		
		// Vue des attributs du doc selectionné
		constraint.gridx = 0;
		constraint.fill = GridBagConstraints.NONE;
		constraint.anchor = GridBagConstraints.CENTER;
		this.add(this.propertiesDocView, constraint);
	}
	
	private void setMenu(Base model) {
		StaffMemberMenuController c = new StaffMemberMenuController(model, this, this.mainWindow);
		JMenuBar menuBar;
		JMenu menu;
		JMenuItem item;
		
		menuBar = new JMenuBar();
		
		menu = new JMenu("Fichier");
		
		item  = new JMenuItem("Changer mon mot de passe");
		item.setName(StaffMemberView.MENU_CHANGE_PASS);
		item.addActionListener(c);
		menu.add(item);
		
		item  = new JMenuItem("Déconnection");
		item.setName(StaffMemberView.MENU_DECO);
		item.addActionListener(c);
		menu.add(item);
		
		item  = new JMenuItem("Quitter");
		item.setName(StaffMemberView.MENU_QUIT);
		item.addActionListener(c);
		menu.add(item);
		
		menuBar.add(menu);
		
		menu = new JMenu("Utilisateurs");
		
		item  = new JMenuItem("Ajouter un utilisateur");
		item.setName(StaffMemberView.MENU_ADD_USER);
		item.addActionListener(c);
		menu.add(item);
		
		item  = new JMenuItem("Rechercher un utilisateur");
		item.setName(StaffMemberView.MENU_SEARCH_USER);
		item.addActionListener(c);
		menu.add(item);
		
		menuBar.add(menu);
		
		menu = new JMenu("Documents");
		
		item  = new JMenuItem("Ajouter un document");
		item.setName(StaffMemberView.MENU_ADD_DOC);
		item.addActionListener(c);
		menu.add(item);
		
		item  = new JMenuItem("Rechercher un document");
		item.setName(StaffMemberView.MENU_SEARCH_DOC);
		item.addActionListener(c);
		menu.add(item);
		
		menuBar.add(menu);
		
		menu = new JMenu("?");
		
		item  = new JMenuItem("A propos...");
		item.setName(StaffMemberView.MENU_ABOUT);
		item.addActionListener(c);
		menu.add(item);
		
		menuBar.add(menu);
		
		this.mainWindow.setJMenuBar(menuBar);
	}
	
	@Override
	public void setDocSearchView() {
		this.remove(this.resultUserView);
		this.add(this.resultDocView, this.ctrResultView);
		this.validate();
	}
	
	@Override
	public void setUserSearchView() {
		this.remove(this.resultDocView);
		this.add(this.resultUserView, this.ctrResultView);
		this.validate();
	}
	
	@Override
	public void setUserPropertiesView() {
		this.remove(this.propertiesDocView);
		this.add(this.propertiesUserView, this.ctrPropertiesView);
		this.validate();
		this.repaint();
	}
	
	@Override
	public void setDocPropertiesView() {
		this.remove(this.propertiesUserView);
		this.add(this.propertiesDocView, this.ctrPropertiesView);
		this.validate();
		this.repaint();
	}
	
	public void logout() {
		this.model.delObserver(this.borrowView);
		this.model.delObserver(this.resultDocView);
		this.model.delObserver(this.resultUserView);
		this.resultDocView.delObserver(this.propertiesDocView);
		this.resultUserView.delObserver(this.propertiesUserView);
		this.resultUserView.delObserver(this.borrowView);
		this.borrowView.delObserver(this.propertiesDocView);
		
		this.model.logout();
		this.mainWindow.disconnect();
	}
}
